#include "devmon/driver_abi.h"
#include "mc/log.h"
#include "nv_l40_card.h"
#include "nv_l40_gpu.h"
#include "nv_l40_memory.h"
#include <iostream>
#include <memory>

#ifndef DEVICE_DRIVER_NAME
#define DEVICE_DRIVER_NAME "nv_l40"
#endif

using namespace dev;

// NVIDIA GPU Card 设备创建和生命周期管理
extern "C" {

driver_handle_t create_nv_l40_card(void* service, const char* name) {
    try {
        if (service == nullptr) {
            return nullptr;
        }
        nv_l40_card* device = new nv_l40_card();
        device->set_service(static_cast<mc::engine::service*>(service));
        device->set_object_name(name);
        return (void*)device;
    } catch (const std::exception& e) {
        elog("Error creating NvidiaGpuCard: ${error}", ("error", e.what()));
        return nullptr;
    }
}

status_t init_nv_l40_card(driver_handle_t device, void* csr_object, void* connector) {
    nv_l40_card*      device_object  = static_cast<nv_l40_card*>(device);
    mc::mutable_dict* csr_object_ptr = static_cast<mc::mutable_dict*>(csr_object);
    mc::dict*         connector_ptr  = static_cast<mc::dict*>(connector);
    bool              ret            = device_object->init(*csr_object_ptr, *connector_ptr);
    if (!ret) {
        return STATUS_ERROR;
    }
    return STATUS_OK;
}

status_t start_nv_l40_card(driver_handle_t device) {
    nv_l40_card* device_object = static_cast<nv_l40_card*>(device);
    device_object->start();
    return STATUS_OK;
}

status_t stop_nv_l40_card(driver_handle_t device) {
    nv_l40_card* device_object = static_cast<nv_l40_card*>(device);
    device_object->stop();
    return STATUS_OK;
}

// NVIDIA GPU 处理器设备
driver_handle_t create_nv_l40_gpu(void* service, const char* name) {
    try {
        if (service == nullptr) {
            return nullptr;
        }
        nv_l40_gpu* device = new nv_l40_gpu();
        device->set_service(static_cast<mc::engine::service*>(service));
        device->set_object_name(name);
        return (void*)device;
    } catch (const std::exception& e) {
        elog("Error creating NvidiaGpu: ${error}", ("error", e.what()));
        return nullptr;
    }
}

status_t init_nv_l40_gpu(driver_handle_t device, void* csr_object, void* connector) {
    nv_l40_gpu*       device_object  = static_cast<nv_l40_gpu*>(device);
    mc::mutable_dict* csr_object_ptr = static_cast<mc::mutable_dict*>(csr_object);
    mc::dict*         connector_ptr  = static_cast<mc::dict*>(connector);
    bool              ret            = device_object->init(*csr_object_ptr, *connector_ptr);
    if (!ret) {
        return STATUS_ERROR;
    }
    return STATUS_OK;
}

status_t start_nv_l40_gpu(driver_handle_t device) {
    nv_l40_gpu* device_object = static_cast<nv_l40_gpu*>(device);
    device_object->start();
    return STATUS_OK;
}

status_t stop_nv_l40_gpu(driver_handle_t device) {
    nv_l40_gpu* device_object = static_cast<nv_l40_gpu*>(device);
    device_object->stop();
    return STATUS_OK;
}

// NVIDIA GPU 显存设备
driver_handle_t create_nv_l40_memory(void* service, const char* name) {
    try {
        if (service == nullptr) {
            return nullptr;
        }
        nv_l40_memory* device = new nv_l40_memory();
        device->set_service(static_cast<mc::engine::service*>(service));
        device->set_object_name(name);
        return (void*)device;
    } catch (const std::exception& e) {
        elog("Error creating NvidiaGpuMemory: ${error}", ("error", e.what()));
        return nullptr;
    }
}

status_t init_nv_l40_memory(driver_handle_t device, void* csr_object, void* connector) {
    nv_l40_memory*    device_object  = static_cast<nv_l40_memory*>(device);
    mc::mutable_dict* csr_object_ptr = static_cast<mc::mutable_dict*>(csr_object);
    mc::dict*         connector_ptr  = static_cast<mc::dict*>(connector);
    bool              ret            = device_object->init(*csr_object_ptr, *connector_ptr);
    if (!ret) {
        return STATUS_ERROR;
    }
    return STATUS_OK;
}

status_t start_nv_l40_memory(driver_handle_t device) {
    nv_l40_memory* device_object = static_cast<nv_l40_memory*>(device);
    device_object->start();
    return STATUS_OK;
}

status_t stop_nv_l40_memory(driver_handle_t device) {
    nv_l40_memory* device_object = static_cast<nv_l40_memory*>(device);
    device_object->stop();
    return STATUS_OK;
}

// 设备驱动定义
device_driver_t nv_l40_card_device_driver = {.device_name = "PCIeGpuCard",
                                             .ctor        = create_nv_l40_card,
                                             .init        = init_nv_l40_card,
                                             .start       = start_nv_l40_card,
                                             .stop        = stop_nv_l40_card};

device_driver_t nv_l40_gpu_device_driver = {.device_name = "GraphicsController",
                                            .ctor        = create_nv_l40_gpu,
                                            .init        = init_nv_l40_gpu,
                                            .start       = start_nv_l40_gpu,
                                            .stop        = stop_nv_l40_gpu};

device_driver_t nv_l40_memory_device_driver = {.device_name = "Memory",
                                               .ctor        = create_nv_l40_memory,
                                               .init        = init_nv_l40_memory,
                                               .start       = start_nv_l40_memory,
                                               .stop        = stop_nv_l40_memory};

// 驱动注册表
device_driver_t nv_l40_driver[] = {nv_l40_card_device_driver, nv_l40_gpu_device_driver,
                                   nv_l40_memory_device_driver};

status_t register_device_driver(device_driver_t** device_driver, uint8_t* count) {
    *device_driver = nv_l40_driver;
    *count         = sizeof(nv_l40_driver) / sizeof(nv_l40_driver[0]);
    return STATUS_OK;
}
}